package com.ruoyi.common.core.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.allocator.RedisIdAllocator;

public class SnowflakeUtils {
    private static final RedisIdAllocator idAllocator = new RedisIdAllocator();
    private static final long DATA_CENTER_ID = idAllocator.allocateDataCenterId();
    private static final long WORKER_ID = idAllocator.allocateWorkerId(DATA_CENTER_ID);
    private static final Snowflake SNOWFLAKE = IdUtil.createSnowflake(WORKER_ID, DATA_CENTER_ID);

    public static long nextId() {
        return SNOWFLAKE.nextId();
    }

    public static void releaseIds() {
        idAllocator.releaseWorkerId(DATA_CENTER_ID, WORKER_ID);
    }
}
